package com.jyxd.web.util;

import com.jyxd.web.error.JyxdException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

import static com.jyxd.web.util.ExecuteUtil.isEmpty;

public class DateUtil {


    /**
     * 判断时间是否是整点数据
     * @param dateStr
     * @return
     */
    public static Boolean  wholePointFlag(String dateStr){

        if(dateStr==null || "".equals(dateStr)){
            return false;
        }
        if(dateStrIsValid(dateStr,"yyyy-MM-dd HH:mm")){
            dateStr=dateStr+":00";
        }
        Date date=null;
        try{
            date= DateUtil.yyyyMMddHHmmssSdfToDate(dateStr);
        }catch(Exception e){
            return false;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        if(minute==0 && second==0 ){
            return true;
        }
        return false;
    }

    /**
     * 计算n分钟以前的时间
     *
     * @param date  开始时间
     * @param minutes 时长
     * @return
     */
    public static Date getBeforeHoursDate(Date date, Long minutes) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, -minutes.intValue());
        return c.getTime();
    }
    /**
     * 计算n月以后的时间
     *
     * @param date  开始时间
     * @param month 时长
     * @return
     */
    public static Date getLaterMonthDate(Date date, Long month) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, month.intValue());
        return c.getTime();
    }

    /**
     * 计算n小时以后的时间
     *
     * @param date  开始时间
     * @param hours 时长
     * @return
     */
    public static Date getLaterHoursDate(Date date, Long hours) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR, hours.intValue());
        return c.getTime();
    }

    /**
     * 计算n小时以后的时间
     *
     * @param date   开始时间
     * @param minute 时长
     * @return
     */
    public static Date getLaterMinuteDate(Date date, Long minute) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, minute.intValue());
        return c.getTime();
    }

    public static String yyyyMMddHHmmssSdfToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String yyyyMMddHHmmSdfToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(date);
    }

    public static Date yyyyMMddHHmmSdfToDate(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.parse(time);
    }

    public static Date yyyyMMddHHmmSdfToSpecialDate(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.parse(time);
    }

    public static Date yyyyMMddHHmmColonSdfToSpecialDate(String time) throws ParseException {
        if (isEmpty(time)) return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH:mm:ss");
        return sdf.parse(time);
    }

    public static Date yyyyMMddHHmmssSdfToDate(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(time);
    }

    public static String yyyyMMddSdfToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static Date yyyyMMddSdfToDate(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(time);
    }

    public static String hhmmssSdfToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);
    }

    public static Date hhmmSdfToDate(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.parse(time);
    }

    public static String hhmmSdfToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(date);
    }

    public static String hhSdfToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        return sdf.format(date);
    }



    public static String mmSdfToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("mm");
        return sdf.format(date);
    }

    public static boolean judgeSize(Date date1, Date date2) {
        if (date1.getTime() < date2.getTime()) {
            return true;
        }
        return false;
    }


    /**
     * 验证字符串是否为指定日期格式
     *
     * @param rawDateStr 待验证字符串
     * @param pattern    日期字符串格式, 例如 "yyyy-MM-dd"
     * @return 有效性结果, true 为正确, false 为错误
     */
    public static boolean dateStrIsValid(String rawDateStr, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            // 转化为 Date类型测试判断
            Date date = dateFormat.parse(rawDateStr);
            return rawDateStr.equals(dateFormat.format(date));
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 判断时间格式是否为 yyyy-MM-dd HH:mm:ss
     *
     * @param rawDateStr
     * @return
     */
    public static boolean dateStrIsValidYYYYMMddHHmmss(String rawDateStr) {
        return dateStrIsValid(rawDateStr, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 判断时间格式是否为 yyyy-MM-dd HH:mm
     *
     * @param rawDateStr
     * @return
     */
    public static boolean dateStrIsValidYYYYMMddHHmm(String rawDateStr) {
        return dateStrIsValid(rawDateStr, "yyyy-MM-dd HH:mm");
    }

    /**
     * 判断时间格式是否为 yyyy-MM-dd HH
     *
     * @param rawDateStr
     * @return
     */
    public static boolean dateStrIsValidYYYYMMddHH(String rawDateStr) {
        return dateStrIsValid(rawDateStr, "yyyy-MM-dd HH");
    }

    /**
     * 判断时间格式是否为 yyyy-MM-dd
     *
     * @param rawDateStr
     * @return
     */
    public static boolean dateStrIsValidYYYYMMdd(String rawDateStr) {
        return dateStrIsValid(rawDateStr, "yyyy-MM-dd");
    }


    /**
     * 判断时间格式是否为 yyyy-MM-dd
     *
     * @param rawDateStr
     * @return
     */
    public static boolean dateStrIsValidYyyyMMddHHmm(String rawDateStr) {
        return dateStrIsValid(rawDateStr, "yyyy-MM-dd");
    }

    /**
     * 判断时间格式是否为 yyyy-MM-dd
     *
     * @param rawDateStr
     * @return
     */
    public static boolean dateStrIsValidyyyyMMddHHmmColon(String rawDateStr) {
        return dateStrIsValid(rawDateStr, "yyyyMMddHH:mm:ss");
    }

    /**
     * 根据不同的时间格式转换对应的Date
     *
     * @param rawDateStr
     * @return
     */
    public static Date strConversionDate(String rawDateStr) {
        try {
            if (dateStrIsValidYYYYMMddHHmmss(rawDateStr))
                return yyyyMMddHHmmssSdfToDate(rawDateStr);
            if (dateStrIsValidYYYYMMddHHmm(rawDateStr))
                return yyyyMMddHHmmSdfToDate(rawDateStr);
            if (dateStrIsValidYYYYMMdd(rawDateStr))
                return yyyyMMddSdfToDate(rawDateStr);
            if (dateStrIsValidYyyyMMddHHmm(rawDateStr))
                return yyyyMMddHHmmSdfToSpecialDate(rawDateStr);
            if (dateStrIsValidyyyyMMddHHmmColon(rawDateStr))
                yyyyMMddHHmmColonSdfToSpecialDate(rawDateStr);
            return null;
        } catch (Exception e) {
            throw new JyxdException("时间格式错误");
        }
    }

    public static String getAgeByBirth(String birthdayStr) {
        int age;
        try {
            Date birthday = yyyyMMddHHmmssSdfToDate(birthdayStr);
            Calendar now = Calendar.getInstance();
            now.setTime(new Date());// 当前时间
            Calendar birth = Calendar.getInstance();
            birth.setTime(birthday);
            if (birth.after(now)) {//如果传入的时间，在当前时间的后面，返回0岁
                age = 0;
            } else {
                age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
                if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
                    age += 1;
                }
            }
            return "" + age;
        } catch (Exception e) {//兼容性更强,异常后返回数据
            return "0";
        }
    }

    public static String getSumTime(Date startTime, Date endTime) {
        if (Objects.isNull(startTime)) {
            return "0小时";
        }
        if (Objects.isNull(endTime)) {
            return "0小时";
        }
        if (endTime.getTime() < startTime.getTime()) {
            return "0小时";
        }
        Long minuteTime = (endTime.getTime() - startTime.getTime()) / 1000 / 60;
        Long hourTime = minuteTime / 60;
        Long remainderTime = minuteTime % 60;
        if (remainderTime > 0) {
            return hourTime + "小时" + remainderTime + "分";
        } else {
            return hourTime + "小时";
        }
    }
    public static String getAgeByBirthNew(String birthdayStr,int type) {
        int age;
        try {
            Date birthday = yyyyMMddHHmmssSdfToDate(birthdayStr);
            //Calendar now = Calendar.getInstance();
            //now.setTime(new Date());// 当前时间
            Calendar birth = Calendar.getInstance();
            //Calendar：日历
            /*从Calendar对象中或得一个Date对象*/
            Calendar cal = Calendar.getInstance();
            /*把出生日期放入Calendar类型的bir对象中，进行Calendar和Date类型进行转换*/
            Calendar bir = Calendar.getInstance();
            bir.setTime(birthday);
            /*如果生日大于当前日期，则抛出异常：出生日期不能大于当前日期*/
            if(cal.before(birthday)){
                throw new IllegalArgumentException("The birthday is before Now,It‘s unbelievable");
            }
            /*取出当前年月日*/
            int yearNow = cal.get(Calendar.YEAR);
            int monthNow = cal.get(Calendar.MONTH)+1;
            int dayNow = cal.get(Calendar.DAY_OF_MONTH);
            /*取出出生年月日*/
            int yearBirth = bir.get(Calendar.YEAR);
            int monthBirth = bir.get(Calendar.MONTH)+1;
            int dayBirth = bir.get(Calendar.DAY_OF_MONTH);
            /*大概年龄是当前年减去出生年*/
            age = yearNow - yearBirth;
            /*如果出当前月小与出生月，或者当前月等于出生月但是当前日小于出生日，那么年龄age就减一岁*/
            if(monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)){
                age--;

            }
            if(type==2){
                // 获取月数差值
                int monthInterval = (monthNow + 12) - monthBirth;
                if (dayNow < dayBirth) {
                    monthInterval--;
                }
                monthInterval %= 12;
                int monthsDiff = Math.abs(age * 12 + monthInterval);
                return monthsDiff+"";
            }
            return age+"";
        } catch (Exception e) {//兼容性更强,异常后返回数据
            return "0";
        }
    }
    public static void main(String[] args) throws Exception{
        //String ageByBirth = getAgeByBirthNew("1992-07-12 00:00:00",1);
       // System.out.println(ageByBirth+"年龄");
       // String ageByBirth2 = getAgeByBirthNew("1992-06-13 00:00:00",2);
        //System.out.println(ageByBirth2+"月龄");
       // String dateStr="2022-02-02 16:00";
        //Boolean flag1 = wholePointFlag(dateStr);
        //String dateStr2="2022-02-02 02:00:10";
       // Boolean flag2 = wholePointFlag(dateStr2);
       // System.out.println(flag2);

    }

}
